"ui";

var builder = new android.app.AlertDialog.Builder(activity);
builder.setMessage("脚本需要打开“无障碍服务”才能运行\n请在随后的设置中找到“一键申报”并开启无障碍服务\n\n*脚本运行过程中可按音量上键强制停止\n\n**第一次使用建议先查看项目Gitee页面");
builder.setCancelable(false);
builder.setPositiveButton("开启无障碍服务", new android.content.DialogInterface.OnClickListener({
    onClick: function (dialog, which) {
        dialog.dismiss();
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
}));
builder.setNegativeButton("Gitee页面", new android.content.DialogInterface.OnClickListener({
    onClick: function (dialog, which) {
        app.openUrl("https://gitee.com/zoy283/one-click-e-wx");
        exit();
    }
}));

if (auto.service == null) {
    //创建AlertDialog对象
    dialog = builder.create();
    dialog.show();
    dialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE).setTextColor(android.graphics.Color.RED);
};

var thread = threads.start(function () {
    setInterval(function () {
        if (auto.service != null) {
            main();
            threads.shutDownAll();
            exit();
            ui.finish();
        }
    }, 100);
});


var cnt = 0;
function main() {

    setScreenMetrics(1080, 2340);//自适应分辨率

    var LaunchAttempt = 1;
    LaunchAttempt = app.launch("com.tencent.mm");
    if (!LaunchAttempt)
        LaunchAttempt = app.launchApp("微信");
    if (!LaunchAttempt) {
        toast("未找到 微信");
        toast("脚本已自动退出")
        exit();
    }

    while (!className("android.widget.TextView").text("通讯录").exists()) {
        CountAndCheck();
    }
    cnt = 0;

    click("通讯录");
    while (!className("android.widget.TextView").text("浙江理工大学").exists()) {
        CountAndCheck();
    }
    cnt = 0;

    click("浙江理工大学");
    while (!className("android.widget.TextView").text("E浙理").exists()) {
        CountAndCheck();
    }
    cnt = 0;

    click("E浙理");
    while (!text("健康申报").exists()) {
        CountAndCheck();
    }
    cnt = 0;

    click("健康申报");
    while (!text("※我要打卡※").exists()) {
        CountAndCheck();
    }
    cnt = 0;

    var b = text("※我要打卡※").findOne().bounds();
    click(b.centerX(), b.centerY());
    while (!text("学号").exists()) {
        CountAndCheck();
    }
    cnt = 0;

    sleep(1000);
    click("正常");
    click("37度以下");
    for (var i = 0; i < 2; i++)
        click("绿色", i);
    for (i = 2; i < 15; i++)
        click("否", i);
    sleep(1000);
    SetAddress("省", "浙江省");
    SetAddress("市", "杭州市");
    SetAddress("区", "江干区");
    setText(0, "浙江理工大学");

    toastLog("操作完成，请自行核对");
    toastLog("脚本已自动退出");
}

function SetAddress(region, address) {
    var regx;
    switch (region) {
        case "省":
            regx = 535;
            break;
        case "市":
            regx = 750;
            break;
        case "区":
            regx = 955;
            break;
    }
    click(regx, 724);   //点击对应区域
    while (!text("搜索").exists())
        ;
    /* var b = text("搜索").findOne().bounds();
    log(b);
    click(b.centerX(), b.centerY()); */
    sleep(300);
    click(500, 420);
    input(10, address);
    sleep(2000);
    click(350, 540);
    sleep(1000);
}

function CountAndCheck() {
    log(cnt++);
    sleep(100);
    if (cnt > 100) {
        toast("脚本长时间未能继续，已自动退出");
        exit();
    }
}